﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Xiao.Application.Dto;
using Xiao.Application.Services.User;
using Xiao.Util;
using Xiao.Code.Entities.Users;
using Xiao.Web.Code;
using Cx.NetCoreUtils.Exceptions;
using System.Linq;

namespace Xiao.Web.Controllers
{
    /// <summary>
    /// 好友模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorization]
    public class FriendsController : XiaoController
    {
        private readonly IRedisUtil _redisUtil;
        private readonly IFriendsService _friendsService;
        private readonly IFriendRecordService _friendRecordService;
        public FriendsController(
            IRedisUtil redisUtil,
            IFriendsService friendsService,
            IFriendRecordService friendRecordService
            )
        {
            _redisUtil = redisUtil;
            _friendsService = friendsService;
            _friendRecordService = friendRecordService;
        }
        /// <summary>
        /// 获取好友列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<FriendsDto>> GetFriendsList()
        {
            return await _friendsService.GetFriendsList(User.Id);
        }
        /// <summary>
        /// 申请好友
        /// </summary>
        /// <param name="friend"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Guid> ApplyForFrien(FriendRecordDto friend)
        {
            friend.ProposerId = User.Id;
            return await _friendRecordService.ApplyForFrien(friend);
        }
        /// <summary>
        /// 好友申请状态更改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="statue"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> ApplicationStatus(Guid id, sbyte statue)
        {
            return await _friendRecordService.ApplicationStatus(id, statue);
        }
        /// <summary>
        /// 获取好友申请记录
        /// </summary>
        /// <param name="statue"></param>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PaginationModel<IList<FriendRecordDto>>> GetFriendRecordsList(sbyte statue=-1, int pageNo=1, int pageSize=20)
        {
            var data =await _friendRecordService.GetFriendRecordsList(User.Id, statue,pageNo,pageSize);
            return new PaginationModel<IList<FriendRecordDto>>(data.Item1, data.Item2);
        }
        /// <summary>
        /// 获取在线好友
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<FriendsDto>> GetOnLineFriend()
        {
            var data =await _friendsService.GetFroemdIds(User.Id);
            var friendIds = data.Select(a => a.FriendId.ToString()).ToList();
            var onLineIds =await _redisUtil.SDiffAsync<string>("OnLine");
            onLineIds= onLineIds.Where(a=>friendIds.Contains(a)).ToArray();
            return data.Where(a => onLineIds.Contains(a.FriendId.ToString())).ToList();
        }
    }
}
